﻿// Decompiled with JetBrains decompiler
// Type: UnityEngine.Jobs.IJobParallelForTransformExtensions
// Assembly: UnityEngine.CoreModule, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: E6AE6B5A-2752-4B07-9528-C8B77C62CE79
// Assembly location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.dll
// XML documentation location: C:\Users\windys\Unity\2021.3.39f1c1\Editor\Data\Managed\UnityEngine\UnityEngine.CoreModule.xml

using System;
using System.Runtime.InteropServices;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using Unity.Jobs.LowLevel.Unsafe;

#nullable disable
namespace UnityEngine.Jobs
{
  /// <summary>
  ///   <para>Extension methods for IJobParallelForTransform.</para>
  /// </summary>
  public static class IJobParallelForTransformExtensions
  {
    public static unsafe JobHandle Schedule<T>(
      this T jobData,
      TransformAccessArray transforms,
      JobHandle dependsOn = default (JobHandle))
      where T : struct, IJobParallelForTransform
    {
      JobsUtility.JobScheduleParameters parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf<T>(ref jobData), IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.Initialize(), dependsOn, ScheduleMode.Batched);
      return JobsUtility.ScheduleParallelForTransform(ref parameters, transforms.GetTransformAccessArrayForSchedule());
    }

    public static unsafe JobHandle ScheduleReadOnly<T>(
      this T jobData,
      TransformAccessArray transforms,
      int batchSize,
      JobHandle dependsOn = default (JobHandle))
      where T : struct, IJobParallelForTransform
    {
      JobsUtility.JobScheduleParameters parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf<T>(ref jobData), IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.Initialize(), dependsOn, ScheduleMode.Batched);
      return JobsUtility.ScheduleParallelForTransformReadOnly(ref parameters, transforms.GetTransformAccessArrayForSchedule(), batchSize);
    }

    public static unsafe void RunReadOnly<T>(this T jobData, TransformAccessArray transforms) where T : struct, IJobParallelForTransform
    {
      JobsUtility.JobScheduleParameters parameters = new JobsUtility.JobScheduleParameters(UnsafeUtility.AddressOf<T>(ref jobData), IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.Initialize(), new JobHandle(), ScheduleMode.Run);
      JobsUtility.ScheduleParallelForTransformReadOnly(ref parameters, transforms.GetTransformAccessArrayForSchedule(), transforms.length);
    }

    [StructLayout(LayoutKind.Sequential, Size = 1)]
    internal struct TransformParallelForLoopStruct<T> where T : struct, IJobParallelForTransform
    {
      public static IntPtr jobReflectionData;

      public static IntPtr Initialize()
      {
        if (IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.jobReflectionData == IntPtr.Zero)
          IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.jobReflectionData = JobsUtility.CreateJobReflectionData(typeof (T), (object) new IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.ExecuteJobFunction(IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.Execute));
        return IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.jobReflectionData;
      }

      public static unsafe void Execute(
        ref T jobData,
        IntPtr jobData2,
        IntPtr bufferRangePatchData,
        ref JobRanges ranges,
        int jobIndex)
      {
        IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.TransformJobData output;
        UnsafeUtility.CopyPtrToStructure<IJobParallelForTransformExtensions.TransformParallelForLoopStruct<T>.TransformJobData>((void*) jobData2, out output);
        int* sortedToUserIndex = (int*) (void*) TransformAccessArray.GetSortedToUserIndex(output.TransformAccessArray);
        TransformAccess* sortedTransformAccess = (TransformAccess*) (void*) TransformAccessArray.GetSortedTransformAccess(output.TransformAccessArray);
        if (output.IsReadOnly == 1)
        {
label_5:
          int beginIndex;
          int endIndex;
          if (!JobsUtility.GetWorkStealingRange(ref ranges, jobIndex, out beginIndex, out endIndex))
            return;
          int num1 = endIndex;
          for (int index1 = beginIndex; index1 < num1; ++index1)
          {
            int index2 = index1;
            int num2 = sortedToUserIndex[index2];
            JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf<T>(ref jobData), num2, 1);
            TransformAccess transform = sortedTransformAccess[index2];
            transform.MarkReadOnly();
            jobData.Execute(num2, transform);
          }
          goto label_5;
        }
        else
        {
          int beginIndex;
          int endIndex;
          JobsUtility.GetJobRange(ref ranges, jobIndex, out beginIndex, out endIndex);
          for (int index3 = beginIndex; index3 < endIndex; ++index3)
          {
            int index4 = index3;
            int num = sortedToUserIndex[index4];
            JobsUtility.PatchBufferMinMaxRanges(bufferRangePatchData, UnsafeUtility.AddressOf<T>(ref jobData), num, 1);
            TransformAccess transform = sortedTransformAccess[index4];
            transform.MarkReadWrite();
            jobData.Execute(num, transform);
          }
        }
      }

      private struct TransformJobData
      {
        public IntPtr TransformAccessArray;
        public int IsReadOnly;
      }

      public delegate void ExecuteJobFunction(
        ref T jobData,
        IntPtr additionalPtr,
        IntPtr bufferRangePatchData,
        ref JobRanges ranges,
        int jobIndex)
        where T : struct, IJobParallelForTransform;
    }
  }
}
